<?php

/**
 * Created by Atom.
 * User: qiliping
 * Date: 2017/2/14
 * Time: 19:21
 * 学校model
 */
class Student_Course_model extends CI_Model
{

  /**
   * 获取学生某个学校的课程
   * @param  [type] $school_id  [description]
   * @param  [type] $student_id [description]
   * @return [type]             [description]
   */
  public function get_student_course_list_by_school($school_id,$student_id)
  {
    $sql = "select  sc.id,cour.title,cate.name as category_name,sc.add_time,sc.status from {$this->db->dbprefix}o2o_student_course sc
left join {$this->db->dbprefix}o2o_course cour on cour.id=sc.course_id
left join {$this->db->dbprefix}course_cate cate on cate.id = cour.cate_id
 where sc.school_id={$school_id} and sc.student_id = {$student_id}";
  return $this->db->query($sql)->result_array();
  }
  /**
   * 获取学员报名的课程详情
   * @param  [type] $id [description]
   * @return [type]     [description]
   */
  public function get_student_course_by_id($id)
  {
    $sql = "select stu_cou.id,stu_cou.order_num,stu_cou.add_time as order_time,stu_cou.course_id,cour.title,stu_cou.status,stu_cou.school_id,stu.*,usr.head_image_url from {$this->db->dbprefix}o2o_student_course stu_cou".
    " left join edu_o2o_course cour on cour.`id` = stu_cou.course_id".
    " left join edu_o2o_student stu on stu.student_id = stu_cou.student_id".
    " left join edu_user usr on usr.id = stu.user_id".
    " where stu_cou.id={$id} LIMIT 1";
    return $this->db->query($sql)->row_array();
  }

  /**
   * 给学员分班
   * @param  [type] $student_course [description]
   * @param  [type] $class_id       [description]
   * @return [type]                 [description]
   */
  public function allot_class_to($student_course_id,$class_id)
  {
    $this->db->trans_start();
    //查询edu_o2o_student_course
    $sql = "SELECT * FROM {$this->db->dbprefix}o2o_student_course WHERE id = {$student_course_id} LIMIT 1";
    $student_course = $this->db->query($sql)->row_array();
    if(empty($student_course)) return -1;
    //插入edu_o2o_class_student
    $class_student = array(
      'class_id' => $class_id,
      'student_id' => $student_course['student_id'],
      'from' => 0,
      'class_title' => '',
      'sort' => '0',
      'status' => '0',
      'add_time' => time(),
    );
    $this->db->insert('o2o_class_student',$class_student);
    //更新edu_o2o_student_course
    $this->db->update('o2o_student_course',array('status' => 1 ),array('id' => $student_course_id ));

    $this->db->trans_complete();

    return 1;
  }
}
